package mysgoog.util;

import java.io.*;
import java.util.*;

import mysgoog.gtp.Vertex;
/**
* @author Fu, Zhongyang
*/
public class DragonDBReader
{
    List<String> pattern = new ArrayList<String>();
    String patternType = "";
    String s = "";
    String ans = "";
    String ansud = "";
    String anslr = "";
    String ansudlr = "";
    int l1 = 0;
    int w1 = 0;
    int l = 0;
    int w = 0;
    List<Integer> x;
    List<Integer> y;
    int count = 0;
    boolean isPattern = true;

    public static void main(String arg[])
    {
        new DragonDBReader("pattern\\LargeDragonPattern.db");
    }

    public DragonDBReader(String filename)
    {
        read(filename);
        FileManager.write("pattern\\LargeDragonPattern.dbpattern", s);
    }

    public void read(String filename)
    {
        BufferedReader reader = null;
        try
        {
            File file = new File(filename);
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null)
            {
                //System.err.println(text);
            	//System.out.println(count++);
                parse(text);
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void parse(String content){
    	if (content.length() == 0) return;
    	String[] token = content.split(" ");
    	switch (token[0].charAt(0)){
    		case '#':
    			return;
    		case '^':
    			isPattern = false;
    			l1 = 0;
    			w1 = 0;
    			return;
    		case ';':
    			//
    			return;
    		case ':':
    			return;
    		case 'P':
    			if (l != 0){
	    			s += l + " " + w + " " + patternType + " " + ans + patternSize(0) + "\n";
	    			s += l + " " + w + " " + patternType + " " + ansud + patternSize(1) + "\n";
	    			s += l + " " + w + " " + patternType + " " + anslr + patternSize(2) + "\n";
	    			s += l + " " + w + " " + patternType + " " + ansudlr + patternSize(3) + "\n";
    			}
    			ans = "";
    			ansud = "";
    			anslr = "";
    			ansudlr = "";
    			l = 0;
    			w = 0;
    			x = new ArrayList<Integer>();
    			y = new ArrayList<Integer>();
    			isPattern = true;
    			patternType = token[1];
    			//patternType = token[1].substring(0, 2);
    			return;
    		case '>':
    			return;
    		case 'E':
    			s += l + " " + w + " " + patternType + " " + ans + patternSize(0) + "\n";
    			s += l + " " + w + " " + patternType + " " + ansud + patternSize(1) + "\n";
    			s += l + " " + w + " " + patternType + " " + anslr + patternSize(2) + "\n";
    			s += l + " " + w + " " + patternType + " " + ansudlr + patternSize(3) + "\n";
    			return;
    		default : 
    			if (isPattern) parsePattern(content);
    			else parsePos(content);
    			return;
    	}
    	
    }
    
    public void parsePattern(String content){ 
    	content = content.replace('-', '|');
    	content = content.replace('+', '|');    	
    	w = content.length();
    	l++;
    	ans += content;
    	ansud = content + ansud;
    	StringBuilder builder = new StringBuilder(content);
    	builder.reverse();
    	anslr += builder;
    	ansudlr = builder + ansudlr;
    }
    
    public void parsePos(String content){
    	content = content.replace('-', '.');
    	content = content.replace('+', '.');
    	content = content.replace('o', '.');    
    	content = content.replace('O', '.');    
    	content = content.replace('x', '.');
    	content = content.replace('X', '.');
    	content = content.replace('?', '.');     	
    	for (int i = 0; i < content.length(); i++){
    		if (content.charAt(i) != '.'){
    			x.add(l1);
    			y.add(i);
    		}
    	}
    	w1 = content.length();
    	l1++;
    }
    
    public String patternSize(int i){
    	String ans = new String();
    	switch (i){
    		case 0:
    			for (int j = 0; j < x.size(); j++)
    				ans += " " + x.get(j).toString() + " " + y.get(j).toString();
    			return ans;
    		case 1:
    			for (int j = 0; j < x.size(); j++)
    				ans += " " + (l - 1 - x.get(j)) + " " + y.get(j).toString();
    			return ans;    			
    		case 2:
    			for (int j = 0; j < x.size(); j++)
    				ans += " " + x.get(j).toString() + " " + (w - 1 - y.get(j));
    			return ans;    			
    		case 3:
    			for (int j = 0; j < x.size(); j++)
    				ans += " " + (l - 1 - x.get(j)) + " " + (w - 1 - y.get(j));
    			return ans;    			
    	}
    	return null;
    }
    
}

